﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Puddy.Resources;
using Puddy.ViewModels;
using Windows.ApplicationModel;
using Windows.Storage;
using System.Threading.Tasks;
using System.Windows;
using Puddy.Utils;

namespace Puddy.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            this.LocationList = new ObservableCollection<LocationViewModel>();
            this.RingToneList = new ObservableCollection<RingToneViewModel>();
            this.ListItemStateOn = new ObservableCollection<LocationViewModel>();
            
        }

        /// <summary>
        /// 
        /// </summary>
        ObservableCollection<LocationViewModel> locationList;

        public ObservableCollection<LocationViewModel> LocationList
        {
            get { return locationList; }
            set { locationList = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        private ObservableCollection<LocationViewModel> listItemStateOn;

        public ObservableCollection<LocationViewModel> ListItemStateOn
        {
            get { return listItemStateOn; }
            set { listItemStateOn = value; }
        }

        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        ObservableCollection<RingToneViewModel> ringToneList;

        public ObservableCollection<RingToneViewModel> RingToneList
        {
            get { return ringToneList; }
            set { ringToneList = value; }
        }

        public bool IsLocationListDataLoaded
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public async Task LoadListLocation()
        {
            ObservableCollection<LocationViewModel> locations = await RestoreData.GetLocationList() as ObservableCollection<LocationViewModel>;
            if (locations != null)
            {

                foreach (LocationViewModel location in locations)
                {
                    this.LocationList.Add(location);

                    if (location.State == Constants.ON_STATE)
                    {
                        this.listItemStateOn.Add(location);
                    }
                }
            }

            this.IsLocationListDataLoaded = true;
        }


        public bool IsSettingAlarmDataLoaded
        {
            get;
            private set;
        }

        /// <summary>
        /// 
        /// </summary>
        public async Task loadListRingTone()
        {
           //get the storage for your app

            StorageFolder store = Windows.Storage.ApplicationData.Current.LocalFolder;

            StorageFolder folder = null;

            try
            {
                folder = await store.GetFolderAsync("RingTones");
            }
            catch (System.IO.FileNotFoundException)
            {}

            if (folder == null)
            {
                StorageFolder install = Windows.ApplicationModel.Package.Current.InstalledLocation;
                folder = await install.GetFolderAsync("Assets\\RingTones");
                 
            }


            //StorageFolder folder = await Package.Current.InstalledLocation.GetFolderAsync("Assets/RingTones");

            var ringTones = await folder.GetFilesAsync();
            foreach (StorageFile file in ringTones)
            {
                this.RingToneList.Add(new RingToneViewModel(file.Name.Substring(0, file.Name.Length - 4), "Assets/RingTones/" + file.Name, false ));
            }
            //// Sample data; replace with real data
            //this.RingToneList.Add(new RingToneViewModel() { Name = "Name1", Path = "Duong le thi rieng, tp hcm", IsSelected = false });
            //this.RingToneList.Add(new RingToneViewModel() { Name = "Name12", Path = "Duong le thi rieng, tp hcm1", IsSelected = false });
            //this.RingToneList.Add(new RingToneViewModel() { Name = "Name3", Path = "Duong le thi rieng, tp hcm2", IsSelected = false });
            //this.RingToneList.Add(new RingToneViewModel() { Name = "Name4", Path = "Duong le thi rieng, tp hcm3", IsSelected = false });
            //this.RingToneList.Add(new RingToneViewModel() { Name = "Name5", Path = "Duong le thi rieng, tp hcm4", IsSelected = false });
            //this.RingToneList.Add(new RingToneViewModel() { Name = "Name6", Path = "Duong le thi rieng, tp hcm5", IsSelected = false });
            //this.RingToneList.Add(new RingToneViewModel() { Name = "Name1", Path = "Duong le thi rieng, tp hcm6", IsSelected = false });
            //this.RingToneList.Add(new RingToneViewModel() { Name = "Name1", Path = "Duong le thi rieng, tp hcm7", IsSelected = false });
            //this.RingToneList.Add(new RingToneViewModel() { Name = "Name1", Path = "Duong le thi rieng, tp hcm8", IsSelected = false });
            //this.RingToneList.Add(new RingToneViewModel() { Name = "Name1", Path = "Duong le thi rieng, tp hcm9", IsSelected = false });
            //this.RingToneList.Add(new RingToneViewModel() { Name = "Name1", Path = "Duong le thi rieng, tp hcm11", IsSelected = false });
            //this.RingToneList.Add(new RingToneViewModel() { Name = "Name1", Path = "Duong le thi rieng, tp hcm10", IsSelected = false });

            IsSettingAlarmDataLoaded = true;
        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}